package guda.house.biz.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import guda.house.biz.bo.ElecFeeBO;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.form.bill.BillElecEditForm;
import guda.house.biz.form.bill.BillElecForm;
import guda.house.dal.dao.ElecRuleDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.BillElecBiz;
import guda.house.dal.dao.BillElecDAO;
import guda.house.dal.dataobject.BillElecDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class BillElecBizImpl implements BillElecBiz{

    private final static Logger logger = LoggerFactory.getLogger(BillElecBizImpl.class);

    @Autowired
    private BillElecDAO billElecDAO;
    @Autowired
    private ElecFeeBO elecFeeBO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            BillElecDO billElecDO = billElecDAO.getBillElecById(id);
            bizResult.data.put("billElecDO", billElecDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query BillElec error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = billElecDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<BillElecDO> billElecList = billElecDAO.selectForPage(baseQuery);
            bizResult.data.put("billElecList", billElecList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view BillElec list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            billElecDAO.delBillElecById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete billElec error", e);
        }
        return bizResult;
    }

    public BizResult create(BillElecForm billElecForm) {
        BizResult bizResult = new BizResult();


        BillElecDO billElecDO = billElecForm.toDO();
        billElecDO.setRecordTotal(billElecForm.getRecordCurrent() - billElecForm.getRecordLast());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(billElecDO.getGmtStart());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        //判断是否已经录入该月份的账单
        List<BillElecDO> bill = billElecDAO.getBill(billElecForm.getSuiteId(), year, month);
        if(bill.size()>0){
            bizResult.code = CommonResultCode.BILL_ELEC_REPEAT;
            return bizResult;
        }

        billElecDO.setRecordYear(year);
        billElecDO.setRecordMonth(month);
        billElecDO.setGmtRecord(new Date());
        billElecDO.setIsPayed(0);
        // 计算费用
        long payTotal = elecFeeBO.countElecFee(billElecForm.getVillageId(),billElecForm.getSuiteId(), billElecDO.getRecordTotal());
        billElecDO.setPayTotal(payTotal);
        try {
            long id = billElecDAO.insert(billElecDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create BillElec error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(BillElecEditForm billElecEditForm) {
        BizResult bizResult = new BizResult();
        BillElecDO billElecDO = billElecEditForm.toDO();
        billElecDO.setRecordTotal(billElecEditForm.getRecordCurrent() - billElecEditForm.getRecordLast());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(billElecDO.getGmtStart());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;

        //判断是否已经录入该月份的账单
        List<BillElecDO> bill = billElecDAO.getBill(billElecEditForm.getSuiteId(), year, month);
        if(bill.size()>1){
            bizResult.code = CommonResultCode.BILL_ELEC_REPEAT;
            return bizResult;
        }else if(bill.size() == 1){
            if(bill.get(0).getBillId().intValue()!= billElecEditForm.getBillId().intValue()){
                bizResult.code = CommonResultCode.BILL_ELEC_REPEAT;
                return bizResult;
            }
        }

        billElecDO.setRecordYear(year);
        billElecDO.setRecordMonth(month);
        billElecDO.setGmtRecord(new Date());
        billElecDO.setIsPayed(0);

        long payTotal = elecFeeBO.countElecFee(billElecEditForm.getVillageId(), billElecEditForm.getSuiteId(),billElecDO.getRecordTotal());
        billElecDO.setPayTotal(payTotal);
        try {
            billElecDAO.updateByIdSelective(billElecDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update BillElec error", e);
        }
        return bizResult;
    }


}
